// pages/teacher/myclass/teacherStuCourse/teacherStuCourse.js
var app = getApp();
import {
  post,
  restPost
} from "../../../../asserts/js/request";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    filter: {
      status: 0,
      keyword: ""
    },
    statusLables: [{
        id: "lableB",
        name: "已完成",
        class: "ystd-button-b",
        value: 1
      },
      {
        id: "lableC",
        name: "未完成",
        class: "ystd-button-b",
        value: -1
      },
      {
        id: "lableA",
        name: "全 部",
        class: "ystd-button-a",
        value: 0
      },
    ],
    top: app.globalData.bottom + 'px',
    courseListOrigin: [],
    courseList: [],
    classId: null,
    className: null,
    studentId: null,
  },
  changeStatus: function (e) {
    var statusLable = e.currentTarget.dataset.item;

    this.data.filter.status = statusLable.value;

    for (var i = 0; i < this.data.statusLables.length; i++) {
      if (this.data.statusLables[i].id == statusLable.id) {
        this.data.statusLables[i].class = "ystd-button-a";
      } else {
        this.data.statusLables[i].class = "ystd-button-b";
      }
    }

    this.setData({
      statusLables: this.data.statusLables
    });
    this.refreshQeuryShow();
  },
  changeName: function (e) {
    this.data.filter.keyword = e.detail.value.trim();
    this.refreshQeuryShow();
  },
  /**
   * 更新filter之后，调用方法过滤信息
   */
  refreshQeuryShow: function () {
    console.log("course filter")
    console.log(this.data.filter)
    this.data.courseList = [];

    for (var i = 0; i < this.data.courseListOrigin.length; i++) {
      var kwFlag = false;
      var statFlag = false;
      var tmpCourse = this.data.courseListOrigin[i];

      if (this.data.filter.keyword.length > 0) {
        if (tmpCourse.courseName.indexOf(this.data.filter.keyword) >= 0) {
          kwFlag = true
        }
      } else {
        kwFlag = true;
      }

      if (this.data.filter.status == 0) {
        statFlag = true;
      } else if (this.data.filter.status == 1) {
        if (tmpCourse.featureNum == tmpCourse.activeNum) {
          statFlag = true;
        }
      } else {
        if (tmpCourse.featureNum != tmpCourse.activeNum) {
          statFlag = true;
        }
      }

      if (kwFlag && statFlag) {
        this.data.courseList.push(tmpCourse);
      }
    }

    this.setData({
      courseList: this.data.courseList
    });

    // this.selectComponent(".processLine").buidView();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;

    var stu = wx.getStorage({
      key: 'choosenStudent',
    });
    var avatarUrl = (stu == null ? null : stu.avatarUrl);
    this.setData({
      classId: options.classId,
      className: options.className,
      avatarUrl: avatarUrl,
      studentName: options.studentName,
      studentId: options.studentId
    });

    // 仅查询陪练课程
    post('teacher/queryStudentCourses', {
      classId: this.data.classId,
      studentId: this.data.studentId,
      courseNature: "402"
    }).then(function (data) {
      _this.refreshCourse(data.courseInfo);
    }).catch(function (e) {
      console.log(e)
      wx.hideLoading({
        success: (res) => {},
      })
    })
  },
  //  刷新课程信息，传入参入为整个教材的列表
  refreshCourse: function (courseList) {
    var ta=0, tt=0;
    for (var i = 0; i < courseList.length; i++) {
      var tmpCourse = courseList[i];
      var featureNum = 0;
      var activeNum = 0;
      for (var j = 0; j < tmpCourse.features.length; j++) {
        if (tmpCourse.features[j].active) {
          activeNum++;
          ta++;
        }
        tt++;
        featureNum++;
      }
      tmpCourse.featureNum = featureNum;
      tmpCourse.activeNum = activeNum;

      tmpCourse.showFeature = 'none'; // 绑定默认不显示课程关键动作
      courseList[i] = tmpCourse;
    }

    this.data.courseListOrigin = courseList;
    this.updateClassProcessLine(ta, tt);
    this.refreshQeuryShow();
  },
  showFeature: function(e){
    console.log(e.currentTarget.dataset.id);
    let courseId= e.currentTarget.dataset.id;
    let _this= this;
    for (var i = 0; i < _this.data.courseList.length; i++) {
      if (courseId == _this.data.courseList[i].courseId) {
        if(_this.data.courseList[i].showFeature== ''){
          _this.data.courseList[i].showFeature= 'none';
        }else{
          _this.data.courseList[i].showFeature= '';
        }
        
      }
    }
    _this.setData({
      courseList: _this.data.courseList
    });
  },
  back: function () {
    wx.navigateBack({
      delta: 0,
    })
  },
  // 更新班级课程进度动作条
  updateClassProcessLine: function(active, total){
    this.selectComponent("#classProcess").updateProcess(active, total);
  },
  // 更新关键动作进度
  updateProcess: function (e) {
    let _this = this;
    let _item = e.currentTarget;

    console.log(e.currentTarget.dataset.feature)
    restPost("teacher/updateStudentCourseFeatures", {
      featureId: e.currentTarget.dataset.feature.featureId,
      studentId: this.data.studentId,
      classId: this.data.classId
    }).then(data => {
      wx.showToast({
        title: '更新完成情况成功',
      })
      var tt=0;
      var ta=0;
      for (var i = 0; i < _this.data.courseListOrigin.length; i++) {
        if (data.feature.courseId == _this.data.courseListOrigin[i].courseId) {
          console.log(data.feature.courseId)         
          for (var j = 0; j < _this.data.courseListOrigin[i].features.length; j++) {
            if (data.feature.featureId == _this.data.courseListOrigin[i].features[j].featureId) {
              _this.data.courseListOrigin[i].features[j] = data.feature;
            }

            tt++;
            if(_this.data.courseListOrigin[i].features[j].active){
              ta++;
            }
          }
        }else{
          for (var j = 0; j < _this.data.courseListOrigin[i].features.length; j++) {
            tt++;
            if(_this.data.courseListOrigin[i].features[j].active){
              ta++;
            }
          }
        }
      }

      for (var i = 0; i < _this.data.courseList.length; i++) {
        if (data.feature.courseId == _this.data.courseList[i].courseId) {
          console.log(data.feature.courseId)
          var activeNum= 0;
          for (var j = 0; j < _this.data.courseList[i].features.length; j++) {
            if (data.feature.featureId == _this.data.courseList[i].features[j].featureId) {
              _this.data.courseList[i].features[j] = data.feature;
            }
            if(_this.data.courseList[i].features[j].active){
              activeNum++;
            }
          }
          _this.data.courseList[i].activeNum= activeNum;
        }
      }
      _this.setData({
        courseList: _this.data.courseList
      });

      console.log({
        a:ta,
        t:tt
      })
      _this.updateClassProcessLine(ta, tt);
      //_this.refreshCourse(_this.data.courseListOrigin);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})